package com.joycessh.search.index.config;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.HbaseUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author Joycessh
 * @Description
 * @Date 4/27/19 4:31 PM
 */
@Service
public class HbaseTest {

    @Autowired
    private HbaseTemplate hbaseTemplate;

    public void run() throws IOException {
        String record = "";
        org.apache.hadoop.conf.Configuration configuration = hbaseTemplate.getConfiguration();
        Scan scan = new Scan();
        ResultScanner results = HbaseUtils.getHTable("testcrawlid_webpage",
                configuration).getScanner(scan);

        try {
            for(Result result : results){
                for (Cell cell : result.rawCells()) {
                    StringBuffer stringBuffer = new StringBuffer()
                            .append(Bytes.toString(cell.getRow())).append("\t")
                            .append(Bytes.toString(cell.getFamily())).append("\t")
                            .append(Bytes.toString(cell.getQualifier())).append("\t")
                            .append(Bytes.toString(cell.getValue())).append("\n");
                    String str = stringBuffer.toString();
                    record += str;
                }
            }
        } finally {
            if (results != null) {
                results.close();
            }
        }
        System.out.println(record);

    }

}
